package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.quality.QualityWarehouseWarrant;
import com.ruoyi.system.mapper.QualityWarehouseWarrantMapper;
import com.ruoyi.system.service.IQualityWarehouseWarrantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author GE-LV5
 * @create 2023/3/1 11:29
 */
@Service
public class QualityWarehouseWarrantServiceImpl extends ServiceImpl<QualityWarehouseWarrantMapper, QualityWarehouseWarrant> implements IQualityWarehouseWarrantService {

    @Autowired
    QualityWarehouseWarrantMapper qualityWarehouseWarrantMapper;

    @Override
    public List<QualityWarehouseWarrant> selectTransferSlipList(QualityWarehouseWarrant warrant) {
        return qualityWarehouseWarrantMapper.selectTransferSlipList(warrant);
    }

    @Override
    public int deleteSetAreaByIds(List<Long> ids) {
        return 0;
    }

    @Override
    public int deleteEquipmentLedgerById(Long id) {
        return qualityWarehouseWarrantMapper.deleteEquipmentLedgerById(id);
    }

    @Override
    @Transactional
    public int insertEquipmentLedger(QualityWarehouseWarrant warrant) {
        if (warrant == null || warrant.equals("")) {
            throw new CustomException("添加失败！");
        }
        Integer receiptsNumber = warrant.getReceiptsNumber();  //实收数量
        Integer returnNumber = warrant.getReturnNumber(); //退货数量
        Integer supplied = (receiptsNumber + returnNumber);//来料数量
        warrant.setIncomingNumber(supplied);
        warrant.setCreateTime(DateUtils.getNowDate());
        warrant.setCreateBy(SecurityUtils.getUsername());
        warrant.setUpdateBy(SecurityUtils.getUsername());
        warrant.setUpdateTime(DateUtils.getNowDate());
        return qualityWarehouseWarrantMapper.insertEquipmentLedger(warrant);
    }


    @Override
    public int updateOrdersManual(QualityWarehouseWarrant warrant) {
        if (warrant == null || warrant.equals("")) {
            throw new CustomException("修改失败！");
        }
        warrant.setUpdateTime(DateUtils.getNowDate());
        warrant.setUpdateBy(SecurityUtils.getUsername());
        return qualityWarehouseWarrantMapper.updateOrdersManual(warrant);
    }

    @Override
    public QualityWarehouseWarrant warehouseById(Long id) {
        return qualityWarehouseWarrantMapper.warehouseById(id);
    }
}
